<?php
/**
 * 小程序首页配置
 * @Author   jiating
 */
namespace app\admin\controller;

use \think\Request;
use think\Controller;
use app\admin\model\Product;
use app\admin\model\Producttype;
use app\admin\model\Storeproduct;
use app\admin\model\Groupproduct;
use app\admin\model\Integralproducts;
use app\admin\model\Article;
use app\admin\model\Store;
use app\admin\model\Image;

class Layer extends Controller{

    //商品弹出层数据接口
    public function product() {
        $request = Request::instance();
        $param = $request->param();
        // return $param;

        $columns = $param['order'][0]['column'];
        if( $columns === '0') {  //排序
            $order = 'a.orderNo';
        } else {
            $order = $param['columns'][$columns]['data'].' '.$param['order'][0]['dir'];
        }

        $storeId = $param['storeId'];
        $start = $param['start'];  //每页起始条数
        $length = $param['length'];  //每页条数

        $where = ['store_id'=>$storeId];
        if($param['search']['value']) {
            $search = json_decode($param['search']['value'], true);
            if ($search['typeId']) {  //搜索
                $where['typeId'] = ['=', $search['typeId']];
            }
        }

        $plist = Storeproduct::alias('a')->join('product b','a.product_id=b.id')->field('b.imgUrl,b.id, b.title,b.currentPrice,b.specClass,a.created')->where($where)->order($order)->limit($start,$length)->select();
        $count = Storeproduct::alias('a')->join('product b','a.product_id=b.id')->field('b.imgUrl,b.id, b.title,b.currentPrice,b.specClass,a.created')->where($where)->count();
        $res = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $plist,
            'recordsFiltered' => $count,
            'recordsTotal' => $count
        ];
        return json($res);
    }

    /**
     * 商品分类弹出层数据接口
     * @Author   jiating
     * @DateTime 2018-05-08
     * @return
     */
    public function productType() {
        $request = Request::instance();
        $param = $request->param();
        // return $param;

        $start = $param['start'];  //每页起始条数
        $length = $param['length'];  //每页条数
        $columns = $param['order'][0]['column'];
        $order = $param['columns'][$columns]['data'].' '.$param['order'][0]['dir'];
        $list = Producttype::where('tenantId',session('TENANT_ID'))->order($order)->limit($start,$length)->select();
        $count = Producttype::where('tenantId',session('TENANT_ID'))->count();

        $res = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count
        ];
        return json($res);
    }

    /**
     * 拼团商品弹出层数据接口
     * @Author   jiating
     * @DateTime 2018-06-08
     * @return
     */
    public function groups() {
        $request = Request::instance();
        $param = $request->param();
        // return $param;
        $order = 'a.sort ASC';

        $storeId = $param['storeId'];
        $start = $param['start'];  //每页起始条数
        $length = $param['length'];  //每页条数

        $where = ['storeId'=>$storeId,'status'=>['NEQ','2']];
        $search = json_decode($param['search']['value'],true);
        if($search['typeId']){  //搜索
            $where['typeId'] = ['like','%'.$search['typeId'].'%'];
        }

        $plist = Groupproduct::alias('a')->join('product b','a.productId=b.id')->field('b.imgUrl,a.id, b.title,b.currentPrice,b.specClass,a.create_time,a.groupSuccCount,b.groupPrice,a.groupDateStr')->where($where)->order($order)->limit($start,$length)->select();
        $count = Groupproduct::where('storeId',$storeId)->count();
        $res = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $plist,
            'recordsFiltered' => $count,
            'recordsTotal' => $count
        ];
        return json($res);
    }

    /**
     * 文章弹出层数据接口
     * @Author   jiating
     * @DateTime 2018-05-08
     * @return 
     */
    public function articles() {
        $request = Request::instance();
        $param = $request->param();
        // return $param;

        $start = $param['start'];  //每页起始条数
        $length = $param['length'];  //每页条数
        $columns = $param['order'][0]['column'];
        $order = $param['columns'][$columns]['data'].' '.$param['order'][0]['dir'];
        $list = Article::where('tenantId',session('TENANT_ID'))->order($order)->limit($start,$length)->select();
        $count = Article::where('tenantId',session('TENANT_ID'))->count();

        $res = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count
        ];
        return json($res);
    }

    /**
     * 积分商品列表数据接口
     * @Author   jiating
     * @DateTime 2018-06-08
     * @return
     */
    public function points() {
        $request = Request::instance();
        $param = $request->param();
        // return $param;
        $order = 'a.sort ASC';
        
        $storeId = $param['storeId'];
        $start = $param['start'];  //每页起始条数
        $length = $param['length'];  //每页条数

        $where = ['storeId'=>$storeId,'status'=>['NEQ','2']];
        $search = json_decode($param['search']['value'],true);
        if($search['typeId']){  //搜索
            $where['typeId'] = ['like','%'.$search['typeId'].'%'];
        }

        $plist = Integralproducts::alias('a')->join('product b','a.productId=b.id')->field('b.imgUrl,b.id, b.title,b.currentPrice,b.specClass,a.create_time,b.integralPrice')->where($where)->order($order)->limit($start,$length)->select();
        $count = Integralproducts::where('storeId',$storeId)->count();
        $res = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $plist,
            'recordsFiltered' => $count,
            'recordsTotal' => $count
        ];
        return json($res);
    }

    //门店列表弹出层数据接口
    public function storeList() {
        $request = Request::instance();
        $param = $request->param();
        // return $param;

        $start = $param['start'];  //每页起始条数
        $length = $param['length'];  //每页条数
        $list = Store::where('tenantId',session('TENANT_ID'))->limit($start,$length)->select();
        $count = Store::where('tenantId',session('TENANT_ID'))->count();

        $res = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count
        ];
        return json($res);
    }


    public function img() {
        $request = Request::instance();
        $param = $request->param();

        $start = $param['start'];  //每页起始条数
        $length = $param['length'];  //每页条数
        $list = Image::field('imgUrl')->where(['tenantId'=>session('TENANT_ID'),'isVideo'=>2])->limit($start,$length)->order('created DESC')->select();
        $count = Image::where('tenantId',session('TENANT_ID'))->count();
        $res = [
            'status' => '1',
            'draw' => $param['draw'],
            'data' => $list,
            'recordsFiltered' => $count,
            'recordsTotal' => $count
        ];
        return json($res);
    }




}